package com.bjfu.controller;

import com.bjfu.dto.LoginRequest;
import com.bjfu.dto.MemberRequestDTO;
import com.bjfu.dto.UpdatePasswordDTO;
import com.bjfu.model.ErrorInfo;
import com.bjfu.model.File;
import com.bjfu.service.GeneralService;
import com.bjfu.service.UserService;
import com.bjfu.utils.Result;
import com.bjfu.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.net.MalformedURLException;
import java.nio.file.Path;

@RestController
@RequestMapping("/General")
public class GeneralController {
    @Autowired
    private GeneralService generalService;

    // 用户登录接口
    @PostMapping("/login")
    public Result login(@RequestBody LoginRequest loginRequest) {
        // 进行登录逻辑处理
        return generalService.login(loginRequest.getUsername(), loginRequest.getPassword());
    }


    /**
     * 用户注册功能：团队成员注册
     * @param memberRequestDTO 注册请求参数
     * @return 注册结果
     */
    @PostMapping("/register")
    public Result registerMember(@RequestBody MemberRequestDTO memberRequestDTO) {
        //@Valid
        System.out.println(memberRequestDTO);
        // 调用业务层逻辑进行注册
        return generalService.registerTeamMember(memberRequestDTO);
    }

    /**
     * 查询用户申请状态
     * @param user_id 用户ID
     * @return 用户状态
     */
    @GetMapping("/viewRegistrationStatus")
    public Result checkRegistrationStatus(@RequestParam Long user_id) {
        // 查询用户状态
        return generalService.checkUserStatus(user_id);
    }
    //获取某类成果
    @GetMapping("/AchievementByCategory")
    public Result AchievementByCategory(@RequestParam Integer category) {
        return generalService.AchievementByCategory(category);
    }
    //获取某类文章
    @GetMapping("/ArticleByCategory")
    public Result ArticleByCategory(@RequestParam Integer category) {
        return generalService.ArticleByCategory(category);
    }
    //根据成员id获取团队成员信息
    @GetMapping("/getMemberInfo")
    public Result getMemberInfo(@RequestParam Long user_id) {
        return generalService.getMemberInfo(user_id);
    }
    //根据成果id获取成果信息
    @GetMapping("/getAchievementInfo")
    public Result getAchievementInfo(@RequestParam Long achievement_id) {
        return generalService.getAchievementInfo(achievement_id);
    }
    //根据文章id获取文章信息
    @GetMapping("/getArticleInfo")
    public Result getArticleInfo(@RequestParam Long article_id) {
        return generalService.getArticleInfo(article_id);
    }
    //根据成员id获取姓名
    @GetMapping("/getMemberName")
    public Result getMemberName(@RequestParam Long user_id) {
        return generalService.getMemberName(user_id);
    }


    // 查询所有团队成员（userinfo表的所有信息）
    @GetMapping("/getMembers")
    public Result getAllTeammembers() {
        return generalService.getAllTeammembers();
    }

    // 查询所有团队成员（userinfo表的所有信息）
    @GetMapping("/getTeam")
    public Result getTeam() {
        return generalService.getTeam();
    }

    //查询所有文章信息（article表）
    @GetMapping("/getArticles")
    public Result getArticles() {
        return generalService.getArticles();
    }

    //获取公开成果
    @GetMapping("/getOpenAchievements")
    public Result getOpenAchievements() {
        return generalService.getOpenAchievements();
    }

    //注销账号
    @PostMapping("/Cancel")
    public Result Cancel(@RequestParam Long user_id) {
        return generalService.Cancel(user_id);
    }
    //文件上传
    @PostMapping("/uploadFile")
    public Result uploadFile(@RequestParam("file")MultipartFile file){
        try {
            // 调用 Service 层处理上传
            return generalService.uploadFile(file);
        } catch (Exception e) {
            return Result.build(null, ResultCodeEnum.UPLOAD_FAILED);
        }
    }
    //文件下载
    // 文件下载接口
    // 文件下载接口
    /*@GetMapping("/downloadFile/{fileId}")
    public  ResponseEntity<Resource> downloadFile(@PathVariable("fileId") Long fileId, HttpServletRequest request) {
        System.out.println("fileId:"+fileId);
        return generalService.downloadFile(fileId,request);
    }*/
    @GetMapping("/downloadFile")
    public ResponseEntity<Resource> downloadFile(@RequestParam("path") String filePath, HttpServletRequest request) {
        System.out.println("filePath: " + filePath);
        return generalService.downloadFile(filePath, request);
    }

    // 根据 type_id 和 type 获取审核理由
    @GetMapping("/getErrorInfo")
    public Result getErrorInfoByTypeIdAndType(
            @RequestParam("type_id") Long typeId,
            @RequestParam("type") Integer type) {
        // 获取错误信息
        return generalService.getErrorInfoByTypeIdAndType(typeId, type);

    }
    //用户修改密码
    @PostMapping("/updateUserPassword")
    public Result updateUserPassword(@RequestBody UpdatePasswordDTO updatePasswordDTO){
        return generalService.updatePassword(updatePasswordDTO);
    }


}
